package com.totobox.apps.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.totobox.apps.Const;
import com.totobox.apps.R;
import com.totobox.apps.common.AboutCustomDialog;
import com.totobox.apps.common.MoveActivity;

public class TotoSettingActivity extends BasicActivity {
	
	private TextView txt_photo_mb;
	private TextView txt_movie_mb;
	private TextView txt_music_mb;
	private TextView txt_etc_mb;
	
	private TextView toto_logout;
	
	
	private CheckBox m_normal_check;
	private CheckBox m_high_check;
	private CheckBox m_auto_check;
	
	private ImageButton btn_notice;
	private ImageButton btn_about;
	
	final private int ABOUT_DIALOG = 0;
	final private int NOTICE_DIALOG = 1;
	final private int OUT_DIALOG = 2;
	
	private AlertDialog logoutAlert;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.my_setting);
        
        super.initTitle();
        
        //하단 메뉴 세팅
        super.setFooterMenu(this);
        
        txt_photo_mb = (TextView) findViewById(R.id.txt_photo_mb);
        txt_movie_mb = (TextView) findViewById(R.id.txt_movie_mb);
        txt_music_mb = (TextView) findViewById(R.id.txt_music_mb);
        txt_etc_mb = (TextView) findViewById(R.id.txt_etc_mb);
        
        
        
        txt_photo_mb.setText(getMB(Const.mainFolder.get("IMG").get("biFileSizeByFolder"))+" MB");
        txt_movie_mb.setText(getMB(Const.mainFolder.get("VOD").get("biFileSizeByFolder"))+" MB");
        txt_music_mb.setText(getMB(Const.mainFolder.get("AUD").get("biFileSizeByFolder"))+" MB");
        txt_etc_mb.setText(getMB(Const.mainFolder.get("ETC").get("biFileSizeByFolder"))+" MB");
        
        toto_logout = (TextView) findViewById(R.id.toto_logout);
        toto_logout.setOnClickListener(this);
        
        btn_notice = (ImageButton) findViewById(R.id.btn_notice);
        btn_about = (ImageButton) findViewById(R.id.btn_about);
        
        
        m_normal_check = (CheckBox) findViewById(R.id.m_normal_check);
        m_high_check = (CheckBox) findViewById(R.id.m_high_check);
        m_auto_check = (CheckBox) findViewById(R.id.m_auto_check);
        
        m_normal_check.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				m_high_check.setChecked(false);
		    	m_auto_check.setChecked(false);
				SharedPreferences pref = TotoSettingActivity.this.getSharedPreferences(Const.APP_NAME, MODE_PRIVATE);
		    	SharedPreferences.Editor editor = pref.edit();
				editor.putBoolean("toto_vod_quality", false);
		    	editor.commit();
			}
		});
        
        m_high_check.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				m_normal_check.setChecked(false);
		    	m_auto_check.setChecked(false);
				SharedPreferences pref = TotoSettingActivity.this.getSharedPreferences(Const.APP_NAME, MODE_PRIVATE);
		    	SharedPreferences.Editor editor = pref.edit();
				editor.putBoolean("toto_vod_quality", false);
		    	editor.commit();
			}
		});
        
        m_auto_check.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				m_high_check.setChecked(false);
				m_normal_check.setChecked(false);
				SharedPreferences pref = TotoSettingActivity.this.getSharedPreferences(Const.APP_NAME, MODE_PRIVATE);
		    	SharedPreferences.Editor editor = pref.edit();
				editor.putBoolean("toto_vod_quality", false);
		    	editor.commit();
			}
		});
        
        btn_notice.setOnClickListener(this);
        btn_about.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		
		if(toto_logout == v){
			showDialog(OUT_DIALOG);
		}
		
		if(btn_notice == v){
			MoveActivity go = new MoveActivity(TotoSettingActivity.this , TotoNoticeListActivity.class);
			go.startActivity();
		}
		
		if(btn_about == v){
			showDialog(NOTICE_DIALOG);
		}
		
		if(btn_title_left == v){
			finish();
		}
	}

	
	/*@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		
		SharedPreferences pref = TotoSettingActivity.this.getSharedPreferences(Const.APP_NAME, MODE_PRIVATE);
    	SharedPreferences.Editor editor = pref.edit();
		if(m_normal_check == buttonView){
			editor.putBoolean("toto_vod_quality", false);
			
			super.showText(this, "m_normal_check 체크");
	    	m_high_check.setChecked(false);
	    	m_auto_check.setChecked(false);
		}
		else if(m_high_check == buttonView){
			
			super.showText(this, "m_high_check 체크");
			
			editor.putBoolean("toto_vod_quality", true);
			m_normal_check.setChecked(false);
	    	m_auto_check.setChecked(false);
		}
		else if(m_auto_check == buttonView){
			
			super.showText(this, "m_auto_check 체크");
			
			editor.putBoolean("toto_vod_quality", false);
			m_normal_check.setChecked(false);
	    	m_high_check.setChecked(false);
		}
    	editor.commit();
		
	}*/

	@Override
	protected Dialog onCreateDialog (int id) {
		switch (id) {
		case ABOUT_DIALOG:
			
			
			return null;
			
		case NOTICE_DIALOG:
			
			Dialog dialog = new AboutCustomDialog(this);
    		dialog.show();
			
			return null;
		case OUT_DIALOG:
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    	builder.setMessage("로그아웃 시 자동 로그인도 해제됩니다.\n로그아웃 하시겠습니까?").setCancelable(true)
	    	       .setPositiveButton("로그아웃", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	        	   
	    	        	   SharedPreferences pref = TotoSettingActivity.this.getSharedPreferences(Const.APP_NAME, MODE_PRIVATE);
	    	        	   SharedPreferences.Editor editor = pref.edit();
	    	        	   editor.putBoolean("totobox_auto", false);
	    	        	   editor.commit();
	    	        	   
	    	        	   MoveActivity go = new MoveActivity(TotoSettingActivity.this , TotoLoginActivity.class);
	    	        	   go.startActivity(TotoSettingActivity.this);
	    	        	   
	    	           }
	    	       })
	    	       .setNegativeButton("취소", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	        	   logoutAlert.dismiss();
	    	    	    	TotoSettingActivity.this.removeDialog(OUT_DIALOG);
	    	           }
	    	       });
	    	logoutAlert = builder.create();
	    	logoutAlert.show();
			
			return logoutAlert;
		default:
			
			return null;
		}
	}
	
	public double getMB(String str){
    	
    	 double mb = 0d;
    	
    	if(str == null)
    		return mb;
    	
    	try{
    		
    		double b = Double.parseDouble(str);
    		mb = Math.round((b/1024)/1024);
    		
    	}catch(Exception e){}
    	
    	return mb;
    }
}